﻿using System;
using msBsc.AppLogic.DataAlc;
using System.Data;

namespace msBsc.WebUi.Custom
{
    public partial class wfBuyScore : msBsc.Cls.BPDealer
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if(!IsPostBack)
            {
                showSocrePrice();
            }
        }

        private void showSocrePrice()
        {
            using (msBsc.AppLogic.DataAlc.CBizShpScorePub objpub = new CBizShpScorePub())
            {
                DataSet ds = objpub.SelectSurplusQty();
                if (ds.Tables[0].Rows.Count <= 0)
                {
                    //this.ExecJScript("$('#divInputInfo').hide();");
                    //this.ExecJScript("$('#divMailSend').show();");
                    this.ToErrPage("商城当前积分库存不足，不能完成完成积分购买操作！");
                }
                int iqtysum = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                { 
                    iqtysum += (int)ds.Tables[0].Rows[i]["SURPLUS_QTY"];
                }
                this.ExecJScript("$('#txtScoreQty').val('" + msBsc.SvRef.SysConst.decScoreUsePrice().ToString() + "');");
                this.ExecJScript("$('#spnSalesPrice').html('每1点积分=￥ " + msBsc.SvRef.SysConst.decScoreUsePrice().ToString() + " 元');");
                this.ExecJScript("$('#spnAllowBuyQty').html('" + iqtysum.ToString() + "');");
            }
        }

        public override void DoAction()
        {
            if (this.strAction == "REGIST_STEP2")
            {
                if (this.GetParamHtValue("J_BuyQty") != string.Empty)
                {
                    string stremqty = this.GetParamHtValue("J_BuyQty");

                    int iBuyqty = 0;
                    if (!int.TryParse(stremqty, out iBuyqty))
                    {
                        this.Alert("输入的购买数量无效，请重新录入！"); 
                        return;
                    }

                    if (iBuyqty==0)
                    {
                        this.Alert("输入的购买数量无效，请重新录入！"); 
                        return;
                    } 

                    
                    System.Transactions.TransactionOptions transactionOption = new System.Transactions.TransactionOptions();
                    transactionOption.IsolationLevel = System.Transactions.IsolationLevel.ReadUncommitted;
                    transactionOption.Timeout = new TimeSpan(0, 10, 0);
                    using (System.Transactions.TransactionScope ts = new System.Transactions.TransactionScope(System.Transactions.TransactionScopeOption.Required, transactionOption))
                    {
                        using (msBsc.AppLogic.DataAlc.CBizShpScoreSalesLog objss = new CBizShpScoreSalesLog())
                        {
                            string strtem = objss.addSalesScoreLog("购买", this.oUserInfo.OperDealerId.Split(',')[0], iBuyqty);
                            this.ExecJScript("window.location.href = '/c_PayCashier.htm?ID=" + strtem + "';");
                        }
                        ts.Complete();
                    }


                    //this.ExecJScript("$('#spncmp').addClass('active');");
                    //this.ExecJScript("$('#divInputInfo').hide();");
                    //this.ExecJScript("$('#divMailSend').show();");

                }
                else
                {
                    this.Alert("输入的购买数量！");
                    return;
                }
            }
        }
    }
}